package com.nattakute.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.nattakute.dao.ClassesDAO;
import com.nattakute.model.Classes;
import com.nattakute.model.Timetable;

@Repository
public class ClassesDAOImpl implements ClassesDAO{

	@Autowired
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@SuppressWarnings("unchecked")
	public List<Classes> getAllClasses() {
		try {				
			List<Classes> listclasses=new ArrayList<Classes>();
			listclasses=(List<Classes>)sessionFactory.getCurrentSession().createQuery("from Classes").list();	
			return  listclasses;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}

	
	public Classes getClassesById(String id) {
		Classes classes = null;
		try {			
			classes = (Classes)sessionFactory.getCurrentSession().createQuery("from Classes where classID = :id").setParameter("id",id ).list().get(0);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return classes;
	}

	
	public Boolean addClasses(Classes classes) {
		try {
			sessionFactory.getCurrentSession().save(classes);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public Boolean deleteClasses(Classes classes) {
		try {
			sessionFactory.getCurrentSession().delete(classes);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public Boolean updateClasses(Classes classes) {
		try {
			sessionFactory.getCurrentSession().update(classes);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	
	public List<Classes> getClassesByDepartmentId(String id) {
		List<Classes> classes=new ArrayList<Classes>();
		Criteria criteriaClass=sessionFactory.getCurrentSession().createCriteria(Classes.class);
		Criteria departmentCriteria=criteriaClass.createCriteria("departmentID");
		departmentCriteria.add(Restrictions.eq("departmentID", id));
		try {
			classes=departmentCriteria.list();
			if(classes.size()==0)
			{
				return null;
			}
			System.out.println("ok");
			System.out.println(classes.get(0).getClassName());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("looser");
			return null;
		}
		return classes;
	}

	
	@SuppressWarnings("unchecked")
	public List<Classes> getClassesByName(String name) {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession().createQuery("from Classes where className= :name ").setParameter("name", name).list();
	}

	
	@SuppressWarnings("unchecked")
	public List<Classes> checkClassHasLearn(String classId) {
		// TODO Auto-generated method stub
		Criteria classHasLearn = sessionFactory.getCurrentSession().createCriteria(Timetable.class);
		classHasLearn.add(Restrictions.eq("haslearn", false));
		return classHasLearn.list();
	}

	
	public Classes classInTimeTable(String TimeTalbeId) {
		// TODO Auto-generated method stub		
		return null;
	}

}
